{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,)\n",
      "(3, 2)\n",
      "int64\n",
      "[[ 1  2]\n",
      " [ 5 10]\n",
      " [ 7  8]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([[1,2],[5,5],[7,8]])\n",
    "b[1,1] = 10\n",
    "print(a.shape)\n",
    "print(b.shape)\n",
    "print(a.dtype)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(b'zhangsan', 20, b'man') (b'lisi', 18, b'woman') (b'wangwu', 30, b'man')]\n",
      "1\n",
      "(3,)\n",
      "3\n",
      "[('name', 'S32'), ('age', '<i4'), ('sex', 'S32')]\n",
      "68\n"
     ]
    }
   ],
   "source": [
    "mydtype=np.dtype({\n",
    "    'names':['name','age','sex'],\n",
    "    'formats':['S32','i4','S32']\n",
    "})\n",
    "\n",
    "persons = np.array([\n",
    "    ('zhangsan',20,'man'),\n",
    "    ('lisi',18,'woman'),\n",
    "    ('wangwu',30,'man')\n",
    "],dtype=mydtype)\n",
    "\n",
    "print(persons)\n",
    "# 秩\n",
    "print(persons.ndim)\n",
    "# 维度\n",
    "print(persons.shape)\n",
    "# 个数\n",
    "print(persons.size)\n",
    "# 类型\n",
    "print(persons.dtype)\n",
    "# 元素大小，字节为单位\n",
    "print(persons.itemsize)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[25895968444448860 23925768161198148]\n",
      " [32370111954616435 18577662001348700]\n",
      " [35466156309086287      261993005105]]\n",
      "[0. 0. 0. 0. 0.]\n",
      "[0 0 0 0]\n",
      "[1. 1. 1.]\n",
      "[1. 1. 1. 1.]\n",
      "[1 3 5]\n",
      "[1 2 3]\n",
      "[[1 1 1]\n",
      " [1 1 1]]\n"
     ]
    }
   ],
   "source": [
    "#空数组\n",
    "xx = np.empty([3,2],dtype=int)\n",
    "print(xx)\n",
    "#0数组\n",
    "zero1 = np.zeros(5)\n",
    "zero2 = np.zeros(4, dtype=int)\n",
    "print(zero1)\n",
    "print(zero2)\n",
    "#1数组\n",
    "one1=np.ones(3)\n",
    "one2=np.ones(4,dtype=float)\n",
    "print(one1)\n",
    "print(one2)\n",
    "#从已有数组创建数组\n",
    "list1=[1,3,5]\n",
    "tuple1=(1,2,3)\n",
    "one = np.ones((2,3), dtype=int)\n",
    "array1 = np.asarray(list1)\n",
    "array2 = np.asarray(tuple1)\n",
    "array3 = np.asarray(one)\n",
    "\n",
    "print(array1)\n",
    "print(array2)\n",
    "print(array3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n",
      "lisi\n",
      "wangwu\n",
      "zhaoliu\n",
      "\n",
      "\n",
      "键: zhangsan , 值 1\n",
      "键: lisi , 值 2\n",
      "键: wangwu , 值 3\n",
      "键: zhaoliu , 值 4\n",
      "100\n",
      "20\n",
      "12.34\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 迭代\n",
    "L = ['zhangsan','lisi','wangwu','zhaoliu']\n",
    "D = {\"zhangsan\":1, \"lisi\":2, \"wangwu\":3, \"zhaoliu\":4}\n",
    "for l in L:\n",
    "    print(l)\n",
    "\n",
    "print('\\n')\n",
    "for k,v in D.items():\n",
    "    print(\"键:\",k,\",\",\"值\",v)\n",
    "\n",
    "print(abs(100))\n",
    "print(abs(-20))\n",
    "print(abs(12.34))\n",
    "print(max(1,2))\n",
    "print(max(2,3,1,-5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[b'H' b'e' b'l' b'l' b'o' b' ' b'w' b'o' b'r' b'l' b'd']\n",
      "[0 1 2 3 4]\n",
      "[0 1 2 3 4]\n",
      "[1. 3. 5. 7. 9.]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# 流的形式读入数组\n",
    "import numpy as np\n",
    "str1 = b\"Hello world\"\n",
    "buffer1 = np.frombuffer(str1,dtype='S1')\n",
    "print(buffer1)\n",
    "# 迭代对象中建立数组\n",
    "range1 = range(5)\n",
    "iter1 = np.fromiter(range1, dtype=int)\n",
    "print(iter1)\n",
    "# 数值范围创建数组\n",
    "myarray1 = np.arange(5)\n",
    "print(myarray1)\n",
    "# 等差数列的数组\n",
    "myarray2 = np.linspace(1,9,5)\n",
    "print(myarray2)\n",
    "\n",
    "# ndarray 数组可以基于0 - n 的下标进行索引，切片对象可以通过内置的slice函数，并设置start,stop及step参数进行\n",
    "a = np.arange(10)\n",
    "print(a)\n",
    "b = a[2:7:2] # 从索引2开始到7停止，间隔为2\n",
    "print(b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组:  [0 1 2 3 4 5]\n",
      "变化后数组:  [[0 1]\n",
      " [2 3]\n",
      " [4 5]]\n",
      "原始数组: \n",
      "[0 1 2]\n",
      "[3 4 5]\n",
      "[6 7 8]\n",
      "迭代后的数组：\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "# 修改数组形状\n",
    "a = np.arange(6)\n",
    "print(\"原始数组: \",a)\n",
    "b = a.reshape(3, 2)\n",
    "print(\"变化后数组: \", b)\n",
    "\n",
    "a = np.arange(9).reshape(3,3)\n",
    "print('原始数组: ')\n",
    "for row in a:\n",
    "    print(row)\n",
    "\n",
    "#对数组中每个元素都进行处理，可以使用flat属性，该属性是一个数组元素迭代器\n",
    "print('迭代后的数组：')\n",
    "for element in a.flat:\n",
    "    print(element)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "[[0 5]\n",
      " [1 6]\n",
      " [2 7]\n",
      " [3 8]\n",
      " [4 9]]\n"
     ]
    }
   ],
   "source": [
    "# 可以对换数组的维度\n",
    "a = np.arange(10).reshape(2, 5)\n",
    "print(a)\n",
    "b = a.transpose()\n",
    "print(b)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组:\n",
      "[[1 2]\n",
      " [3 4]]\n",
      "第二个数组\n",
      "[[5 6]\n",
      " [7 8]]\n",
      "沿轴0连接两个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "沿轴1连接两个数组：\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 连接数组\n",
    "a = np.array([[1,2],[3,4]])\n",
    "print('第一个数组:')\n",
    "print (a)\n",
    "b = np.array([[5,6],[7,8]])\n",
    "print('第二个数组')\n",
    "print(b)\n",
    "\n",
    "print('沿轴0连接两个数组：')\n",
    "print(np.concatenate((a,b)))\n",
    "\n",
    "print('沿轴1连接两个数组：')\n",
    "print(np.concatenate((a,b), axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组:\n",
      "[0 1 2 3 4 5 6 7 8]\n",
      "将数组分为三个大小想等的子数组:\n",
      "[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]\n"
     ]
    }
   ],
   "source": [
    "# 分割数组\n",
    "a = np.arange(9)\n",
    "\n",
    "print('第一个数组:')\n",
    "print(a)\n",
    "\n",
    "print('将数组分为三个大小想等的子数组:')\n",
    "b = np.split(a,3)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组是：\n",
      "[[3 7 5]\n",
      " [8 4 3]\n",
      " [2 4 9]]\n",
      "调用amin()函数\n",
      "[3 3 2]\n",
      "再次调用amin()函数\n",
      "[2 4 3]\n",
      "调用amax()函数\n",
      "9\n",
      "再次调用amax()函数:\n",
      "[8 7 9]\n"
     ]
    }
   ],
   "source": [
    "# amin 计算数组中指定轴的最小值\n",
    "# amax 计算数组中指定轴的最大值\n",
    "# axis =0，对每一列进行操作\n",
    "# axis =1，对每一行进行操作 \n",
    "a = np.array([[3,7,5],[8,4,3],[2,4,9]])\n",
    "print('数组是：')\n",
    "print(a)\n",
    "print('调用amin()函数')\n",
    "print(np.amin(a,1))\n",
    "print('再次调用amin()函数')\n",
    "print(np.amin(a,0))\n",
    "print('调用amax()函数')\n",
    "print(np.amax(a))\n",
    "print('再次调用amax()函数:')\n",
    "print(np.amax(a,axis = 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我们的数组是：\n",
      "[[3 7 5]\n",
      " [8 4 3]\n",
      " [2 4 9]]\n",
      "调用ptp()函数\n",
      "7\n",
      "沿轴1调用ptp()函数:\n",
      "[4 5 7]\n",
      "沿轴0调用ptp()函数:\n",
      "[6 3 6]\n"
     ]
    }
   ],
   "source": [
    "# 数组元素最大值与最小值之差\n",
    "a = np.array([[3,7,5],[8,4,3],[2,4,9]])\n",
    "print('我们的数组是：')\n",
    "print(a)\n",
    "print('调用ptp()函数')\n",
    "print(np.ptp(a))\n",
    "print('沿轴1调用ptp()函数:')\n",
    "print(np.ptp(a, axis = 1))\n",
    "print('沿轴0调用ptp()函数:')\n",
    "print(np.ptp(a, axis = 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组是\n",
      "[[10  7  4]\n",
      " [ 3  2  1]]\n",
      "调用percentile()函数\n",
      "3.5\n",
      "[6.5 4.5 2.5]\n",
      "[7. 2.]\n",
      "[[7.]\n",
      " [2.]]\n"
     ]
    }
   ],
   "source": [
    "# 计算百分位\n",
    "a = np.array([[10,7,4],[3,2,1]])\n",
    "print('数组是')\n",
    "print(a)\n",
    "\n",
    "print('调用percentile()函数')\n",
    "# 50%的分位数，就是a里排序之后的中位数\n",
    "print(np.percentile(a,50))\n",
    "\n",
    "# axis为0，在纵列上求\n",
    "print(np.percentile(a,50,axis=0))\n",
    "# axis为1，在横行上求\n",
    "print(np.percentile(a,50,axis=1))\n",
    "\n",
    "# 保持维度不变\n",
    "print(np.percentile(a,50,axis=1, keepdims=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组是：\n",
      "[[10  7  4]\n",
      " [ 3  2  1]]\n",
      "3.5\n"
     ]
    }
   ],
   "source": [
    "# 计算数组元素的中位数\n",
    "a = np.array([[10,7,4],[3,2,1]])\n",
    "print('数组是：')\n",
    "print(a)\n",
    "print(np.median(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组是：\n",
      "[[10  7  4]\n",
      " [ 3  2  1]]\n",
      "4.5\n"
     ]
    }
   ],
   "source": [
    "# 平均数\n",
    "a = np.array([[10,7,4],[3,2,1]])\n",
    "print('数组是：')\n",
    "print(a)\n",
    "print(np.mean(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组是：\n",
      "[1 2 3 4]\n",
      "调用average()函数：\n",
      "2.5\n",
      "再次调用average()函数\n",
      "2.0\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3,4])\n",
    "print('数组是：')\n",
    "print(a)\n",
    "print('调用average()函数：')\n",
    "print(np.average(a))\n",
    "wts = np.array([4,3,2,1])\n",
    "print('再次调用average()函数')\n",
    "print(np.average(a,weights = wts))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.118033988749895\n",
      "1.25\n"
     ]
    }
   ],
   "source": [
    "# 标准差和方差\n",
    "print(np.std([1,2,3,4]))\n",
    "print(np.var([1,2,3,4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组是:\n",
      "[[3 7]\n",
      " [9 1]]\n",
      "调用sort()函数：\n",
      "[[3 7]\n",
      " [1 9]]\n",
      "按列排序：\n",
      "[[3 1]\n",
      " [9 7]]\n",
      "按行排序：\n",
      "[[3 7]\n",
      " [1 9]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[3,7],[9,1]])\n",
    "print('数组是:')\n",
    "print(a)\n",
    "print('调用sort()函数：')\n",
    "print(np.sort(a))\n",
    "print('按列排序：')\n",
    "print(np.sort(a,axis = 0))\n",
    "print('按行排序：')\n",
    "print(np.sort(a,axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
